package cn.com.connext.scrm.service.mapper;

import cn.com.connext.scrm.domain.master.InteractiveRecord;
import cn.com.connext.scrm.domain.master.Member;
import cn.com.connext.scrm.repository.master.MemberRepository;
import cn.com.connext.scrm.service.dto.InteractiveRecordDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public abstract class InteractiveRecordMapperDecorator implements InteractiveRecordMapper {

    @Autowired
    @Qualifier("delegate")
    private InteractiveRecordMapper delegate;

    @Autowired
    private MemberRepository memberRepository;

    @Override
    public InteractiveRecordDTO toDto(InteractiveRecord interactiveRecord) {
        InteractiveRecordDTO interactiveRecordDTO = delegate.toDto(interactiveRecord);
        if (StringUtils.isNotEmpty(interactiveRecord.getUserId())) {
            String userId = interactiveRecord.getUserId();
            Member member = memberRepository.findOneByUnionId(userId);
            if (null != member) {
                interactiveRecordDTO.setUserName(member.getNickName());
            }
        }
        interactiveRecordDTO.setContent(interactiveRecord.getContent() == null ? "" : interactiveRecord.getContent());
        return interactiveRecordDTO;
    }
}
